package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.DocumentAddDTO;
import com.oa.admin.dto.DocumentListDTO;
import com.oa.admin.dto.DocumentUpdateDTO;
import com.oa.admin.entity.Document;
import com.oa.admin.service.IDocumentService;
import com.oa.admin.vo.DocumentVO;
import com.oa.system.entity.User;
import com.oa.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/document")
@Api(description = "文档表模块")
public class DocumentController extends BaseController {

    private final IDocumentService documentService;

    @Autowired
    public DocumentController(IDocumentService documentService) {
        this.documentService = documentService;
    }


    @Autowired
    private IUserService userService;

    /**
     * 查询列表
     *
     * @param documentListDTO DocumentListDTO
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("获取列表")
    @ApiOperation(value = "获取列表")
    public Resp<DocumentVO> list(@ApiParam("查询条件")DocumentListDTO documentListDTO) {
        User user = userService.selectById(getLoginUserId());
        documentListDTO.setDepartmentId(user.getDepartmentId());
        documentListDTO.setPostId(user.getPostId());
        return new Resp().success(documentService.findDocumentList(documentListDTO));
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("根据ID查询")
    @ApiOperation(value = "根据id查询")
    public Resp<DocumentVO> findById(@ApiParam("id") @PathVariable String id){
        return new Resp().success(documentService.findDocumentById(id));
    }


    /**
     * 新增
     *
     * @param documentAddDTO DocumentAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "文档表对象") @RequestBody(required = false)DocumentAddDTO documentAddDTO) {
        if (documentService.documentAdd(documentAddDTO)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param document Document
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "文档表对象") @RequestBody(required = false) DocumentUpdateDTO document) {
        if (documentService.documentUpdate(document)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Document document = documentService.selectById(id);
        if (document != null && document.getIsDeleted() == 2) {
            document.setIsDeleted(1);
            if (documentService.updateById(document)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
